using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

using InfFront.InfoViews.Base;

namespace InfFront.InfoViews.WinForms
{
    public partial class MainFrame : BaseFrame, IInfoViewPlatformCallbacks
    {
        #region Private Variables
        private RootInfoViewSource set = null;
        BaseSettingsManager settings = null;
        #endregion

        #region Construction/Initialization
        public MainFrame()
        {
            InitializeComponent();
            Disposed += new EventHandler(MainFrame_Disposed);
        }

        private void MainFrame_Disposed(object sender, EventArgs e)
        {
            if (settings != null)
            {
                settings.SaveDatabase();
            }
            if (set != null)
            {
                set.Dispose();
                set = null;
            }
        }

        public void SetSource(RootInfoViewSource source)
        {
            set = source;

            Rebuild();
        }

        public void SetSource(InfFront.UICore.IInfoSource source)
        {
            settings = new BaseSettingsManager();
            set = new RootInfoViewSource(source, settings.RootAccessor);
            set.PlatformCallbacks = this;
            Rebuild();
        }

        #endregion

        private void Rebuild()
        {
            tvSources.Nodes.Clear();
            sourcesImageList.Images.Clear();
            if (set.IsDummy)
                FillTreeNodes(tvSources.Nodes, set.NestedSources);
            else
                CreateTreeNode(tvSources.Nodes, set);
            tvSources.ExpandAll();
            tvSources_AfterSelect(this, null);
        }

        private void CreateTreeNode(TreeNodeCollection parentNodes, InfoViewSource src)
        {
            var node = parentNodes.Add(src.Title);
            node.Tag = src;
            Image icon = src.GetIcon(24, 24, false, false);
            if (icon != null)
            {
                sourcesImageList.Images.Add(icon);
                node.ImageIndex = sourcesImageList.Images.Count - 1;
                node.SelectedImageIndex = node.ImageIndex;
            }
            FillTreeNodes(node.Nodes, src.NestedSources);
        }

        private void FillTreeNodes(TreeNodeCollection nodes, IEnumerable<InfoViewSource> sources)
        {
            foreach (var src in sources)
                CreateTreeNode(nodes, src);
        }

        private void tvSources_AfterSelect(object sender, TreeViewEventArgs e)
        {
            InfoViewSource src = null;
            if (e != null && e.Node != null)
            {
                src = (InfoViewSource)e.Node.Tag;
                if (src.IsDummy)
                    src = null;
            }
            sourceFrame.SetSource(src);
            sourceFrame.Visible = (src != null);
        }

        #region IInfoViewPlatformCallbacks Members

        void IInfoViewPlatformCallbacks.DisplayBackgroundWork(BackgroundWorker worker, InfFront.UICore.IExecutionControl exec)
        {
            worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
            Application.UseWaitCursor = true;
        }

        void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            Application.UseWaitCursor = false;
        }

        void IInfoViewPlatformCallbacks.ExecInMainThread(AsyncCallMethod method, bool waitForFinish)
        {
            if (waitForFinish)
                Invoke(method, null);
            else
                BeginInvoke(method, null);
        }

        void IInfoViewPlatformCallbacks.HandleAsyncCallException(Exception ex)
        {
            throw ex;
        }

        #endregion
    }
}
